
import 'dart:io';

import 'package:syn_4s/core/model/version_model.dart';
import '../constants/sp_key.dart';
import '../model/about_us_model.dart';
import '../model/base/api_result_data.dart';
import '../utils/dio_utils.dart';
import '../utils/sp_utils.dart';
import 'config.dart';

class SettingRequest {
  static Future<ApiResultData<String>> logOut() async {
    String userId = await SPUtils.get(keyUserId);
    final url = "/api/users/logOut?userId=$userId";
    final result = await DioUtils.instance.requestBaseUrl(url);

    ApiResultData<String> apiResultData =
        ApiResultData<String>.fromJson(result, (data) => data as String);
    // 2.json转model
    return apiResultData;
  }

  static Future<ApiResultData<AboutUsModel>> getAboutUs() async {
    final url = "/api/Policies/companyIntroduction";
    final result = await DioUtils.instance.requestBaseUrl(url);

    ApiResultData<AboutUsModel> apiResultData =
    ApiResultData<AboutUsModel>.fromJson(result,  AboutUsModel.fromJson);
    // 2.json转model
    return apiResultData;
  }

  static Future<ApiResultData<String>> changeSite(
      {required String siteId}) async {
    String userId = await SPUtils.get(keyUserId);
    final result = await DioUtils.instance.requestBaseUrl("/api/users/switchSite",
        method: HttpConfig.post, params: {"userId": userId, "siteId": siteId});

    ApiResultData<String> apiResultData =
        ApiResultData<String>.fromJson(result, (data) => data as String);
    // 2.json转model
    return apiResultData;
  }

  static Future<ApiResultData<VersionModel>> getVersion() async {
    // var package = "com.example.syn_4s";
    final result = await DioUtils.instance.requestBaseUrl("/api/AppVersions");

    ApiResultData<VersionModel> apiResultData =
    ApiResultData<VersionModel>.fromJson(result, (data) => VersionModel.fromJson(data));
    // 2.json转model
    return apiResultData;
  }

  static Future<File> downloadFile(String path, File file, Function(int, int)? onReceiveProgress) async {
    final result = await DioUtils.instance.download("/api/utils/files/$path", file, onReceiveProgress: onReceiveProgress);
    return result as File;
  }
}
